package libnetdb.dm.assoc.base;

import java.util.*;
import java.io.Serializable;

/**
 * Since each item in the <code>Itemset</code> is an integer, we
 * must transform a string to an integer. This class map different
 * strings to different integers and also map those integers back to
 * the original strings.
 *
 * @author Cheng-Ru Lin
 * @version 1.0
 */
public class BasicNameSpace 
        implements NameSpace, Serializable{

    private Hashtable nameMap = new Hashtable();
    private ArrayList idMap = new ArrayList();

    public BasicNameSpace() {
    }
    
    public int registerName(String name){
        Integer id = (Integer) nameMap.get(name);
        if(id == null){
            id = new Integer(idMap.size());
            nameMap.put(name, id);
            idMap.add(name);
        }
        return id.intValue();
    }
    
    public String getName(int id){
        return (String) idMap.get(id);
    }
    
    public int getID(String name){
        Integer id = (Integer) nameMap.get(name);
        return id == null ? -1 : id.intValue();        
    }
}